const { ccclass, property } = cc._decorator;
import HeroControl from '../Hero/HeroControl';
import btCdControl from './btCdControl';
import WorldNodeControl from '../World/WorldNodeControl'
import SoundMgr from '../SoundMgr';

@ccclass
export default class PlayHero extends cc.Component {

    @property(cc.Node)
    playBtNode: cc.Node = null;
    @property(cc.Node)
    playKeyNode: cc.Node = null;
    @property(cc.Node)
    SoundNode: cc.Node = null;

    playNode: cc.Node = null;
    BtJump: cc.Node = null;
    BtSpeedUpWay: cc.Node = null;
    BtSpeedUpHero: cc.Node = null;
    BtSkill: cc.Node = null;
    @property(cc.Node)
    worldNode: cc.Node = null;
    @property(cc.Node)
    heroNode: cc.Node = null;
    @property
    isKeyboard: boolean = false;
    @property(cc.Node)
    skillsNode: cc.Node = null;

    _heroControl: HeroControl = null;
    _WorldNodeControl: WorldNodeControl = null;
    _faceTo: number = 1; // -1左，1右
    _runState: boolean = false;
    _SoundMgr: SoundMgr = null;

    onLoad() {
        this._heroControl = this.heroNode.getComponent(HeroControl);
        this._WorldNodeControl = this.worldNode.getComponent(WorldNodeControl);
        this._SoundMgr = this.SoundNode.getComponent(SoundMgr);
        this.setBtNode();
        this.setEvent();
    }
    setBtNode() {
        this.playNode = this.playBtNode;
        if (this.isKeyboard) {
            this.playBtNode.active = false;
            this.playKeyNode.active = true;
            this.playNode = this.playKeyNode;
        }
        else {
            this.playBtNode.active = true;
            this.playKeyNode.active = false;
        }
        this.BtJump = this.playNode.getChildByName("jump");
        this.BtSpeedUpWay = this.playNode.getChildByName("speedUpWay");
        this.BtSpeedUpHero = this.playNode.getChildByName("speedUpHero");
        this.BtSkill = this.playNode.getChildByName("skill");
        let maneger = HeroManager.getInstance();
        let jumpControl = this.BtJump.getComponent(btCdControl);
        jumpControl.cdTime = maneger.jumpCd;
        jumpControl.init();
        let speedUpControl = this.BtSpeedUpWay.getComponent(btCdControl);
        speedUpControl.cdTime = maneger.speedUpWayCd;
        speedUpControl.init();
        let SkillControl = this.BtSkill.getComponent(btCdControl);
        SkillControl.cdTime = maneger.damageCd;
        SkillControl.init();
        let SpeedUpHero = this.BtSpeedUpHero.getComponent(btCdControl);
        SpeedUpHero.cdTime = maneger.speedUpHeroCd;
        SpeedUpHero.init();
    }
    setEvent() {
        if (this.isKeyboard) {
            cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);
            cc.systemEvent.on(cc.SystemEvent.EventType.KEY_UP, this.onKeyUp, this);
            this.BtJump.on("click", this.jump, this);
            this.BtSpeedUpWay.on("click", this.speedUpWay, this);
            this.BtSpeedUpHero.on("click", this.speedUpHero, this);
            this.BtSkill.on("click", this.skill, this);
        }
        else {
            this.BtJump.on("click", this.jump, this);
            this.BtSpeedUpWay.on("click", this.speedUpWay, this);
            this.BtSpeedUpHero.on("click", this.speedUpHero, this);
            this.BtSkill.on("click", this.skill, this);
        }
        let BtLet = this.playNode.getChildByName("left");
        BtLet.on(cc.Node.EventType.TOUCH_START, this.left, this);
        BtLet.on(cc.Node.EventType.TOUCH_CANCEL, this.stop, this);
        BtLet.on(cc.Node.EventType.TOUCH_END, this.stop, this);
        let BtRight = this.playNode.getChildByName("right");
        BtRight.on(cc.Node.EventType.TOUCH_START, this.right, this);
        BtRight.on(cc.Node.EventType.TOUCH_CANCEL, this.stop, this);
        BtRight.on(cc.Node.EventType.TOUCH_END, this.stop, this);
    }
    onKeyDown(event) {
        switch (event.keyCode) {
            case cc.macro.KEY.a:
                this.left();
                break;
            case cc.macro.KEY.d:
                this.right();
                break;
            case cc.macro.KEY.space:
                {
                    let control = this.BtJump.getComponent(btCdControl);
                    if (!control._isCding) {
                        control.btclick();
                        this.jump();
                    }
                    break;
                }
            case cc.macro.KEY.f:
                {
                    let control = this.BtSpeedUpWay.getComponent(btCdControl);
                    if (!control._isCding) {
                        control.btclick();
                        this.speedUpWay();
                    }
                    break;
                }
            case cc.macro.KEY.w:
                {
                    let control = this.BtSpeedUpHero.getComponent(btCdControl);
                    if (!control._isCding) {
                        control.btclick();
                        this.speedUpHero();
                    }
                    break;
                }
            case cc.macro.KEY.s:
                {
                    let control = this.BtSkill.getComponent(btCdControl);
                    if (!control._isCding) {
                        control.btclick();
                        this.skill();
                    }
                    break;
                }
        }
    }
    onKeyUp(event) {
        switch (event.keyCode) {
            case cc.macro.KEY.a:
            case cc.macro.KEY.d:
                this.stop();
                break;
        }
    }
    onDestroy() {
        if (this.isKeyboard) {
            // 取消键盘输入监听
            cc.systemEvent.off(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);
            cc.systemEvent.off(cc.SystemEvent.EventType.KEY_UP, this.onKeyUp, this);
        }
    }
    /**
     * 攻击
     */
    private skill() {
        this._heroControl.skill();
        this._SoundMgr.play("_skill");
    }
    /**
     * 跳
     */
    private jump() {
        if (this._heroControl) {
            this._heroControl.jump();
            this._SoundMgr.play("_jump");
        }
    }
    /**
     * 加速
     */
    private speedUpHero() {
        if (this._heroControl) {
            if (this._heroControl.speedUp())
                this._SoundMgr.play("_send");
        }
    }
    /**
    * 加速
    */
    private speedUpWay() {
        if (this._WorldNodeControl) {
            let hm = HeroManager.getInstance();
            this._SoundMgr.play("_send");
            this._WorldNodeControl.speedUp(HeroHelper.getInstance().uuidWayCollider, hm.speedUpWay * this._faceTo, hm.speedUpWayTime);
        }
    }
    /**
     * 向左走
     */
    private left() {
        if (this._heroControl) {
            if (this._runState && this._faceTo == -1)
                return;
            this._heroControl.walkLeft();
            this._faceTo = -1;
            this._runState = true;
        }
    }
    /**
     * 向右走
     */
    private right() {
        if (this._heroControl) {
            if (this._runState && this._faceTo == 1)
                return;
            this._heroControl.walkRight();
            this._faceTo = 1;
            this._runState = true;
        }
    }
    /**
     * 停止
     */
    private stop() {
        if (this._runState && this._heroControl) {
            this._heroControl.walkStop();
            this._runState = false;
        }
    }
    /**
     * 
     * @param name 禁用
     */
    DisableBt(name: string) {
        let bt = this.playNode.getChildByName(name);
        if (bt) {
            let cdc = bt.getComponent(btCdControl);
            cdc.setDisable();
        }
    }
    /**
     * 
     * @param name 启用
     */
    EnableBt(name: string) {
        let bt = this.playNode.getChildByName(name);
        if (bt) {
            let cdc = bt.getComponent(btCdControl);
            cdc.setEnable();
        }
    }
}
